import copy
import logging
def append_flipped_images(roidb):
    """
    append flipped images to an roidb
    flip boxes coordinates, images will be actually flipped when loading into network
    :param roidb: [image_index]['boxes', 'gt_classes', 'gt_overlaps', 'flipped']
    :return: roidb: [image_index]['boxes', 'gt_classes', 'gt_overlaps', 'flipped']
    """
    logging.info('append flipped images to roidb')
    num_images = len(roidb)
    roidb_r = copy.deepcopy(roidb)
    for i in range(num_images):
        roi_rec = roidb[i]
        boxes = roi_rec['boxes'].copy()
        oldx1 = boxes[:, 0].copy()
        oldx2 = boxes[:, 2].copy()
        boxes[:, 0] = roi_rec['width'] - oldx2 - 1
        boxes[:, 2] = roi_rec['width'] - oldx1 - 1
        assert (boxes[:, 2] >= boxes[:, 0]).all()
        entry = {'image': roi_rec['image'],
                 'height': roi_rec['height'],
                 'width': roi_rec['width'],
                 'boxes': boxes,
                 'gt_classes': roidb[i]['gt_classes'],
                 'gt_overlaps': roidb[i]['gt_overlaps'],
                 'max_classes': roidb[i]['max_classes'],
                 'max_overlaps': roidb[i]['max_overlaps'],
                 'flipped': True}

        roidb_r.append(entry)
    return roidb_r